package de.nak.notendb.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

import de.nak.notendb.model.ExaminationResult;
import de.nak.notendb.model.ExaminationSubject;
import de.nak.notendb.model.Maniple;
import de.nak.notendb.model.Student;
import de.nak.notendb.service.exceptions.NoManipleAvailableException;
import de.nak.notendb.service.exceptions.NoStudentAvailableException;
import de.nak.notendb.service.interfaces.IExaminationResultService;
import de.nak.notendb.service.interfaces.IExaminationSubjectService;
import de.nak.notendb.service.interfaces.IManipleService;
import de.nak.notendb.service.interfaces.IStudentService;

public class ShowExamOverviewAction extends ActionSupport {

	/**
	 * The generated serialVersionUID.
	 */
	private static final long serialVersionUID = -3578165789264547379L;

	/**
	 * 
	 */
	private IManipleService manipleService;
	private IStudentService studentService;
	private IExaminationSubjectService examinationSubjectService;
	private IExaminationResultService examinationResultService;
	private Long selectedManipleId;
	private Maniple selectedManiple;
	private List<Student> students;
	private Long selectedStudentId;
	private Long selectedSubjectId;
	/**
	 * Id of the selected subject
	 */
	private Long id;
	private ExaminationSubject selectedSubject;
	private ExaminationResult[][] results;
	private Integer[] numberTries;
	private Map<Float, Float> grades;
	private ArrayList<ExaminationResult> resultList;

	@Override
	public String execute() throws Exception {
		// load maniple
		try {
			selectedManiple = manipleService.getManiple(selectedManipleId);
		} catch (NoManipleAvailableException exception) {
			return "DATA_NOT_FOUND";
		}

		// load students
		try {
			students = studentService.listStudents(selectedManipleId);
		} catch (NoStudentAvailableException exception) {
			return "DATA_NOT_FOUND";
		}

		// load subject
		selectedSubject = examinationSubjectService.getExaminationSubject(id);
		selectedSubjectId = id;

		// load results
		results = new ExaminationResult[students.size()][3];
		numberTries = new Integer[students.size()];
		List<ExaminationResult> tempResultList;

		for (int nrStudent = 0; nrStudent < students.size(); nrStudent++) {
			Long studentId = students.get(nrStudent).getId();
			tempResultList = examinationResultService
					.listAllValidExaminationResultsOfAStudentAndASubject(
							studentId, id);
			for (int nrTry = 0; nrTry < tempResultList.size(); nrTry++) {
				results[nrStudent][nrTry] = tempResultList.get(nrTry);
			}
			numberTries[nrStudent] = tempResultList.size();
		}

		return SUCCESS;
	}

	public Long getSelectedManipleId() {
		return selectedManipleId;
	}

	public void setSelectedManipleId(Long selectedManipleId) {
		this.selectedManipleId = selectedManipleId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ExaminationResult[][] getResults() {
		return results;
	}

	public void setResults(ExaminationResult[][] results) {
		this.results = results;
	}

	public Maniple getSelectedManiple() {
		return selectedManiple;
	}

	public List<Student> getStudents() {
		return students;
	}

	public ExaminationSubject getSelectedSubject() {
		return selectedSubject;
	}

	public void setManipleService(IManipleService manipleService) {
		this.manipleService = manipleService;
	}

	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	public void setExaminationSubjectService(
			IExaminationSubjectService examinationSubjectService) {
		this.examinationSubjectService = examinationSubjectService;
	}

	public void setExaminationResultService(
			IExaminationResultService examinationResultService) {
		this.examinationResultService = examinationResultService;
	}

	public Integer[] getNumberTries() {
		return numberTries;
	}

	public Long getSelectedStudentId() {
		return selectedStudentId;
	}

	public void setSelectedStudentId(Long selectedStudentId) {
		this.selectedStudentId = selectedStudentId;
	}

	public Map<Float, Float> getGrades() {
		return grades;
	}

	public Long getSelectedSubjectId() {
		return selectedSubjectId;
	}

	public ArrayList<ExaminationResult> getResultList() {
		return resultList;
	}

}
